package org.jeecg.modules.procurement.service.impl;

import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.BeanPlusUtil;
import org.jeecg.modules.procurement.entity.BussMaterialPrice;
import org.jeecg.modules.procurement.entity.BussMaterialPriceDetail;
import org.jeecg.modules.procurement.mapper.BussMaterialPriceDetailMapper;
import org.jeecg.modules.procurement.mapper.BussMaterialPriceMapper;
import org.jeecg.modules.procurement.service.IBussMaterialPriceService;
import org.jeecg.modules.procurement.vo.BussMaterialPriceDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 原料价格执行表
 * @Author: jeecg-boot
 * @Date:   2024-06-13
 * @Version: V1.0
 */
@Service
public class BussMaterialPriceServiceImpl extends ServiceImpl<BussMaterialPriceMapper, BussMaterialPrice> implements IBussMaterialPriceService {

	@Autowired
	private BussMaterialPriceMapper bussMaterialPriceMapper;
	@Autowired
	private BussMaterialPriceDetailMapper bussMaterialPriceDetailMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(BussMaterialPrice bussMaterialPrice, List<BussMaterialPriceDetail> bussMaterialPriceDetailList) {
		BussMaterialPrice price = this.getOne(new LambdaQueryWrapper<BussMaterialPrice>().orderByDesc(BussMaterialPrice::getId).last("limit 1"));
		List<BussMaterialPriceDetailVo> bussMaterialPriceDetailVos = null;
		if(price != null){
			bussMaterialPriceDetailVos = bussMaterialPriceDetailMapper.selectByMainId(price.getId());
		}
		bussMaterialPriceMapper.insert(bussMaterialPrice);
		if(!CollectionUtils.isEmpty(bussMaterialPriceDetailVos)){
//			bussMaterialPriceDetailVos.stream().forEach(item->item.setPriceId(bussMaterialPrice.getId()));
			for (BussMaterialPriceDetailVo vo:bussMaterialPriceDetailVos) {
				BussMaterialPriceDetail bussMaterialPriceDetail = BeanPlusUtil.toBean(vo, BussMaterialPriceDetail.class,
						CopyOptions.create().setIgnoreProperties("id", "createTime", "createBy", "updateBy", "updateTime"));
				bussMaterialPriceDetail.setPriceId(bussMaterialPrice.getId());
				bussMaterialPriceDetailMapper.insert(bussMaterialPriceDetail);
			}
		}
//		if(bussMaterialPriceDetailList!=null && bussMaterialPriceDetailList.size()>0) {
//			for(BussMaterialPriceDetail entity:bussMaterialPriceDetailList) {
//				//外键设置
//				entity.setPriceId(bussMaterialPrice.getId());
//				bussMaterialPriceDetailMapper.insert(entity);
//			}
//		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(BussMaterialPrice bussMaterialPrice,List<BussMaterialPriceDetail> bussMaterialPriceDetailList) {
		bussMaterialPriceMapper.updateById(bussMaterialPrice);
		
		//1.先删除子表数据
		bussMaterialPriceDetailMapper.deleteByMainId(bussMaterialPrice.getId());
		
		//2.子表数据重新插入
		if(bussMaterialPriceDetailList!=null && bussMaterialPriceDetailList.size()>0) {
			for(BussMaterialPriceDetail entity:bussMaterialPriceDetailList) {
				//外键设置
				entity.setPriceId(bussMaterialPrice.getId());
				bussMaterialPriceDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		bussMaterialPriceDetailMapper.deleteByMainId(id);
		bussMaterialPriceMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			bussMaterialPriceDetailMapper.deleteByMainId(id.toString());
			bussMaterialPriceMapper.deleteById(id);
		}
	}
	
}
